package com.ytjj.qmyx.mall.controller;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.HttpClientUtil;
import com.ytjj.qmyx.mall.constants.RedisConstants;
import com.ytjj.qmyx.mall.model.vo.VivoDataResponseVO;
import com.ytjj.qmyx.mall.model.vo.VivoResponseVO;
import com.ytjj.qmyx.mall.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @version 1.0
 *
 */
@RestController
@RequestMapping(("/vivo"))
@Slf4j
@RefreshScope
public class VivoController {
    @Autowired
    private RedisService redisService;

    /**
     * vivo商业开放平台回调接口
     * @param code
     * @param state
     * @return
     */
    @RequestMapping("/callback")
    public String callback(@RequestParam(value = "code",required = false) String code
            ,@RequestParam(value = "state",required = false) String state){
        log.info("=========>收到code: {} ,state :{}" , code,state);
        getAccessTokenByAuthorizationCode(code);
        return "SUCCEED";
    }

    /**
     * 根据authorization_code获取access_token
     * @param authorizationCode
     * @return
     */
    private CommonResult getAccessTokenByAuthorizationCode(String authorizationCode) {
        if (StringUtils.isEmpty(authorizationCode)) {
            return CommonResult.failed("authorizationCode is null!");
        }
        String url = "https://marketing-api.vivo.com.cn/openapi/v1/oauth2/token";
        Map<String, String> param = new HashMap<>();
        param.put("client_id", "20220811017");
        param.put("client_secret", "3FDE25C5CF048E046854C548532E782E26CE8AD3AC787F468376833E90EC3986");
        param.put("grant_type", "code");
        param.put("code", authorizationCode);
//        param.put("redirect_uri", "https://vip.hyshenzhen.com/api/mallv2/yyb_response");
        log.info("===根据authorizationCode获取access_token返回值请求参数：{}", JSONObject.toJSONString(param));
        String resultJson = HttpClientUtil.doGet(url, param);
        log.info("===根据authorizationCode获取access_token返回值：{}", resultJson);
        if (StringUtils.isEmpty(resultJson)) {
            log.info("===获取access_token返回异常==={}");
            return CommonResult.failed("获取access_token异常!");
        }
        VivoResponseVO responseVO = JSONObject.parseObject(resultJson, VivoResponseVO.class);
        log.info("===responseVO==={}", responseVO);
        VivoDataResponseVO dataResponseVO = JSONObject.parseObject(JSONObject.toJSONString(responseVO.getData()), VivoDataResponseVO.class);
        log.info("====dataResponseVO==={}", dataResponseVO);
        redisService.set(RedisConstants.VIVOAUTHTOKEN_KEY + param.get("client_id"), dataResponseVO.getAccess_token());
        redisService.expire(RedisConstants.VIVOAUTHTOKEN_KEY + param.get("client_id"), (dataResponseVO.getToken_date() - System.currentTimeMillis()) / 1000);
        redisService.set(RedisConstants.VIVOREFRESHTOKEN_KEY + param.get("client_id"), dataResponseVO.getRefresh_token());
        redisService.expire(RedisConstants.VIVOREFRESHTOKEN_KEY + param.get("client_id"), (dataResponseVO.getRefresh_token_date() - System.currentTimeMillis()) / 1000);
        return CommonResult.success();
    }

}
